import { defineComponent, reactive, ref, computed } from "vue";
import Page from "@/components/public/page/page";
import { CardWithTitle } from "@/components/public/card/card";
import LTable from "@/components/public/lTable/lTable";
import insureExportService from "@/modules/groupOrderManagement/insureExport.service";
import useNavigator from "@/hooks/useNavigator";
import { useStore as useUserStore } from "@/store/user";
import { Modal } from "ant-design-vue";
import insureExportPageAuth from "./insureExportPage.auth";
import Voucher from "@/views/index/groupOrderManagement/groupOrderEntryPage/popup/voucher/voucher";
import { useRouter } from "vue-router";
export default defineComponent({
  setup() {
    const router = useRouter();
    const navigator = useNavigator();
    const userStore = useUserStore();
    const exportRef = ref(null as any);
    const batchRef = ref(null as any);
    const voucherRef = ref(null as any);
    console.log(!userStore.isSuperAdmin,userStore.channel_level);
    const state = reactive({
      toolbar: {
        left:(!userStore.isSuperAdmin && userStore.type == 1)?["refresh","export","batch"]:["refresh"],
        right: ["filter", "search"],
        export: {
          handler: () => {
            router.push("/groupOrderManagement/insureExportSonPage")
          }
        },
        batch:{
          handler: () => {
            router.push("/groupOrderManagement/insureBatchSonPage")
          }
        }
      },
      dataSource: [],
      columns: [
        {
          title: "ID",
          dataIndex: "id",
          align: "center",
          width: "80px",
        },
        {
          title: "系统订单号",
          dataIndex: "order_no",
          align: "center",
          width: "100px",
        },
        {
          title: "投保单位名称",
          dataIndex: "applicant_name",
          align: "center",
          width: "180px",
        },
        {
          title: "联系人姓名",
          dataIndex: "contact_name",
          align: "center",
          width: "100px",
        },
        {
          title: "联系电话",
          dataIndex: "applicant_mobile",
          align: "center",
          width: "100px",
        },
        {
          title: "参保人数",
          dataIndex: "people_count",
          align: "center",
          width: "80px",
        },
        {
          title: "已承保人数",
          dataIndex: "insured_people_count",
          align: "center",
          width: "100px",
        },
        {
          title: "总金额",
          dataIndex: "sum_premium",
          align: "center",
          width: "80px",
        },
        {
          title: "状态",
          dataIndex:["order_status","label"],
          align: "center",
          width: "100px",
        },
        {
          title: "上传时间",
          dataIndex: "create_time",
          align: "center",
          width: "180px",
        },
        {
          fixed: "right",
          title: "操作",
          dataIndex: "operation",
          align: "center",
          width: "100px",
        },
      ],
      loading: false,
      searchForm: {
        contact_name: {
          type: "input",
          label: "联系人姓名",
          attrs: { placeholder: "请输入联系人姓名" },
        },
        insure_id_no: {
          type: "input",
          label: "被保人证件号",
          attrs: { placeholder: "请输入被保人证件号" },
        },
        order_status: {
          type: "select",
          label: "状态",
          attrs: { placeholder: "请选择状态" },
          datas: [
            { label: "全部承保", value: 1 },
            { label: "待承保", value: 11 },
            { label: "部分承保", value: 110 },
          ],
        },
      },
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
      },
    } as any);
    // 表格状态变更
    const tableChangeHandler = (pag: { pageSize: number; current: number }) => {
      state.pagination.current = pag.current;
      state.pagination.pageSize = pag.pageSize;
    };
    // 选中项变更
    const onSelectChange = (changableRowKeys: any[]) => {
      state.selectedRowKeys = changableRowKeys;
    };
    const rowSelection = computed(() => {
      return {
        selectedRowKeys: state.selectedRowKeys,
        fixed: true,
        onChange: onSelectChange,
      };
    });
    // 服务
    const $service = new insureExportService();
    // 表格节点
    const LTableRef = ref(null as any);
    const DetailRef = ref(null as any);
    const tableRow = (e: any) => {
      LTableRef.value.$fetchTableData();
    };
    const deleteRow = (id:string | number)=>{
      Modal.confirm({
        title: "提示",
        content: "确认取消?",
        onOk: () => {
          $service.delete({
            order_no: id,
          }).then((res: any) => {
            LTableRef.value.$fetchTableData();
          });
        },
      });
    }
    return () => (
      <Page>
        <CardWithTitle title="承保导出">
            <LTable
              ref={LTableRef}
              service={$service}
              authConfig={insureExportPageAuth()}
              toolbar={state.toolbar}
              v-model={state}
            >
              <a-table
                bordered={true}
                scroll={{ x: "max-content" }}
                dataSource={state.dataSource}
                columns={state.columns}
                pagination={state.pagination}
                onChange={tableChangeHandler}
                // row-selection={rowSelection.value}
                loading={state.loading}
                rowKey="id"
                v-slots={{
                  bodyCell: ({ column, record }: any) => {
                    // // 操作栏
                    if (column.dataIndex === "operation") {
                      return (
                        <div class="operation">
                            <a-button
                              type="primary"
                              size="small"
                              onClick={() => {
                                navigator.push({
                                  path: "/groupOrderManagement/insureExportDetailPage",
                                  query: {
                                    order_no: record.order_no,
                                  },
                                });
                              }}
                            >
                              查看详情
                            </a-button>
                            {
                          (record.order_status.value == 10 || record.order_status.value == 11) && (
                            <a-button
                              type="primary"
                              size="small"
                              onClick={() => {
                                voucherRef.value.open(record.order_no, record.order_status.value);
                              }}
                            >
                              查看凭证
                            </a-button>
                          )
                        }
                        </div>
                      );
                    }
                  },
                }}
              />
            </LTable>
        </CardWithTitle>
         <Voucher
          ref={voucherRef}
          onConfirm={() => {
          }}
        />
      </Page>
    );
  },
});